﻿<%@ WebHandler Language="C#" Class="PaidItems" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business.BLL;

public class PaidItems : AjaxHandlerBase {

    //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ：-
    //返回值説明     ：-
    //功能詳細       ：权限哈希表中添加操作与权限组
    //作成者         ：he.jifeng 
    //作成日         ：2010-09-28
    //*******************************************
    public PaidItems()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_PaidItems_Export);
        htTypePermit.Add("exportall", Constants.Permit_PaidItems_Export);
    }

    //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-09-28
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //个人业绩排行
                case "search":
                    strRet = SearchPaidItems(context);
                    break;
                case "exportall":
                    SearchPaidItemsExport(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private void SearchPaidItemsExport(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        string strHTML = string.Empty;

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();

        strParam = context.Request.Params["pagesize"];
       
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["DateType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("DateType", strParam);
        }
        strParam = context.Request.Params["type"];
        if (!string.IsNullOrEmpty(strParam) && strParam != "0")
        {
            dicParams.Add("type", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["ArcTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ArcTime", strParam);
        }

        strParam = context.Request.Params["ContractCityId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ContractCityId", strParam);
        }
        strParam = context.Request.Params["ContractCompanyId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ContractCompanyId", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["userid"]))
        {
            dicParams.Add("UserId", context.Request.Params["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_All))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_City))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_Company))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        //查看部门
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_Depart))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }



        System.Data.DataSet dsBillInfo = ContractFinanceBillInfo.GetPaidItemsExportAll(dicParams);

        strHTML = GetHtml(dsBillInfo.Tables[0]);

        ExportByServce(context, strHTML);
    }
    
    //获得打印全部的数据
    private string GetHtml(System.Data.DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table class=\"tabsorternew table_archive\"><tr>");
        sb.Append("<th style=\"width:120px;\">商圈</th>");
        sb.Append("<th style=\"width:120px;\">门店</th>");
        sb.Append("<th style=\"width:130px;\">成交编号</th>");
        sb.Append("<th style=\"width:80px;\">成交日期</th>");
        sb.Append("<th style=\"width:270px;\">房屋地址</th>");
        sb.Append("<th style=\"width:100px;\">业务员</th>");
        sb.Append("<th style=\"width:100px;\">店长</th>");
        sb.Append("<th style=\"width:100px;\">高级经理</th>");
        sb.Append("<th style=\"width:100px;\">区域经理</th>");
        sb.Append("<th style=\"width:110px;\">总监</th>");
        sb.Append("<th style=\"width:100px;\">标准佣收</th>");
        sb.Append("<th style=\"width:100px;\">应收佣收</th>");
        sb.Append("<th style=\"width:100px;\">折佣</th>");
        sb.Append("<th style=\"width:100px;\">咨询费</th>");
        sb.Append("<th style=\"width:100px;\">点数</th>");
        sb.Append("<th style=\"width:100px;\">实收</th>");
        sb.Append("<th style=\"width:100px;\">分摊应收佣收</th>");
        sb.Append("<th style=\"width:100px;\">分摊折佣</th>");
        sb.Append("<th style=\"width:100px;\">分摊咨询费</th>");
        sb.Append("<th style=\"width:100px;\">分摊实收</th>");
        sb.Append("<th style=\"width:100px;\">收款日期</th>");
        sb.Append("<th style=\"width:100px;\">实收归属日期</th>");
        sb.Append("<th style=\"width:100px;\">业绩归属日期</th>");
        sb.Append("<th style=\"width:120px;\">合同归属公司</th></tr>");

        double point = 0.0;            //点数
        double ApportionRealCommisionFinal = 0.0;     //分摊应收佣收
        double ApportionCommisionFinal = 0.0;      //分摊折佣
        double ApportionConsultingFinal = 0.0;      //分摊咨询费
        double ApportionPay = 0.0;      //分摊实收
        
        foreach (System.Data.DataRow dr in dt.Rows)
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"AreaName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PointDepart",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractCode",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"HouseAddress",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PointEmp",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"DeparterManagerName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"MangerName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"RegionManagerName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"MajordomoName",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"StanCommision",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"RealCommisionFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"CommisionFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ConsultingFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"Point",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayMoney",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ApportionRealCommisionFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ApportionCommisionFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ApportionConsultingFinal",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ApportionPay",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"PayDate",""));
            sb.AppendFormat("<td>{0}.1</td>", DatasetHelper.GetValue(dr,"PayArchDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractArchDate",""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr,"ContractCompanyName",""));
            sb.Append("</tr>");

            point += string.IsNullOrEmpty(dr["Point"].ToString()) ? 0 : double.Parse(dr["Point"].ToString());
            ApportionRealCommisionFinal += string.IsNullOrEmpty(dr["ApportionRealCommisionFinal"].ToString()) ? 0 : double.Parse(dr["ApportionRealCommisionFinal"].ToString());
            ApportionCommisionFinal += string.IsNullOrEmpty(dr["ApportionCommisionFinal"].ToString()) ? 0 : double.Parse(dr["ApportionCommisionFinal"].ToString());
            ApportionConsultingFinal += string.IsNullOrEmpty(dr["ApportionConsultingFinal"].ToString()) ? 0 : double.Parse(dr["ApportionConsultingFinal"].ToString());
            ApportionPay += string.IsNullOrEmpty(dr["ApportionPay"].ToString()) ? 0 : double.Parse(dr["ApportionPay"].ToString());
            
        }
        sb.AppendFormat("<tr><td>总合计</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>{0}</td><td></td><td></td><td></td><td></td><td>{1}</td>", point, ApportionPay);
        sb.Append("<td></td><td></td><td></td><td></td></tr></table>");
        
        return sb.ToString();
    }
    
    //*******************************************
    //功能概要       ：查询实收明细
    //参数説明       ：context 请求上下文
    //返回值説明     ：响应信息
    //功能詳細       ：查询实收明细
    //作成者         ：he.jifeng
    //作成日         ：2010-09-28
    //*******************************************
    private string SearchPaidItems(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractFinanceBillInfo bllBillInfo = new ContractFinanceBillInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["DateType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("DateType", strParam);
        }
        strParam = context.Request.Params["type"];
        if (!string.IsNullOrEmpty(strParam) && strParam!="0")
        {
            dicParams.Add("type", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["ArcTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ArcTime", strParam);
        }

        strParam = context.Request.Params["ContractCityId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ContractCityId", strParam);
        }
        strParam = context.Request.Params["ContractCompanyId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("ContractCompanyId", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["userid"]))
        {
            dicParams.Add("UserId", context.Request.Params["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }
        
        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_All))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_City))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_Company))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        //查看部门
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PaidItems_Search_Depart))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }



        System.Data.DataSet dsBillInfo = ContractFinanceBillInfo.GetPaidItemsByParams(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", JsonHelp.Convert2Json(dsBillInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBillInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();

        return strRet;
    }
}